import { createRouter, createWebHistory } from 'vue-router';
import Login from '../pages/Login.vue';
import Register from '../pages/Register.vue';
import Index from '../pages/Index.vue';
import BusinessList from '../pages/BusinessList.vue';
import Orders from '../pages/Orders.vue';
import BusinessInfo from '../pages/BusinessInfo.vue';
import Payment from '../pages/Payment.vue';
import Profile from '../pages/Profile.vue';
import { loginStatus } from '../stores/loginStatus';

const routes = [
  {
    path: '/',
    redirect: '/login' // 先进入登录
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/index',
    component: Index
  },
  {
    path: '/businessList',
    component: BusinessList
  },
  {
    path: '/orders',
    component: Orders
  },
  {
    path: '/businessinfo',
    component: BusinessInfo
  },
  {
    path: '/payment',
    component: Payment
  },
  {
    path: '/profile',
    component: Profile
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 导航守卫
router.beforeEach((to, from, next) => {
  const publicPages = ['/login', '/register'];
  const authRequired = !publicPages.includes(to.path);

  if (authRequired && !loginStatus.isLoggedIn) {
    next('/login');
  } else {
    next();
  }
});

export default router;